package service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jms.Destination;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ita.ios.jms.protocol.JmsProtocol;

import dao.OrdersDao;
import po.Merchant;
import po.OrderItems;
import po.Orders;
import sender.QueueSender;
import service.OrdersService;
import util.MerchantGrade;
import util.Po2Vo;
import vo.OrdersVo;

@Service(value = "ordersService")
public class OrdersServiceImpl implements OrdersService{
	@Autowired(required=true)
	private OrdersDao ordersDao;

	@Autowired
	private QueueSender queueSender;
	
	@Autowired
	private Destination queueMerchantGrade;
	
	@Transactional
	@Override
	public List<Orders> findOrdersByCid(String cid) {
		return ordersDao.findOrdersByCid(cid);
	}
	@Transactional
	@Override
	public String updateOrdersStatus(Orders orders) {
		return ordersDao.updateOrders(orders);
	}
	@Transactional
	@Override
	public String updateOrdersRecieve(Orders orders) {
		return ordersDao.updateOrders(orders);
	}
	@Transactional
	@Override
	public String updateOrdersGrade(Orders orders) {
		return ordersDao.updateOrders(orders);
	}
	
	@Transactional
	@Override
	public Orders findOrder(String oid) {
		return ordersDao.findOrder(oid);
	}
	@Transactional
	@Override
	public String addOrder(Orders orders) {
		return ordersDao.addOrder(orders);
	}
	
	@Transactional
	@Override
	public Map<Date, Integer> findOrdersTimeGrade(String mid) {
		List<Orders> orders=ordersDao.findOrdersByMid(mid);
		Map<Date, Integer> map=new HashMap<Date,Integer>();
		for (Orders o : orders) {
			map.put(o.getDt(), o.getOgrade());
		}
		return map;
	}

	@Transactional
	@Override
	public List<OrderItems> findOrderItemsByOid(String oid) {
		return ordersDao.findOrder(oid).getOrderItems();
	}
	@Transactional
	@Override
	public List<Orders> findWaitingForReceiveOrdersByCid(String cid) {
		return ordersDao.findWaitingForReceiveOrdersByCid(cid);
	}
	@Transactional
	@Override
	public List<OrdersVo> findHistoricalOrdersVoByCid(String cid) {
		List<Orders> orderList=ordersDao.findHistoricalOrdersByCid(cid);
		List<OrdersVo> ordersVos=new ArrayList<OrdersVo>();
		for(Orders orders:orderList){
		ordersVos.add(Po2Vo.ordersPo2Vo(orders));
		}
		return ordersVos;
	}
	@Transactional
	@Override
	public List<OrdersVo> findWaitingForReceiveOrdersVoByCid(String cid) {
		List<Orders> orderList = ordersDao.findWaitingForReceiveOrdersByCid(cid);	
/*		for (Orders orders : orderList) {
			System.out.println(orders.getOrderItems().get(0).getdName());
		}*/
		List<OrdersVo> ordersVos=new ArrayList<OrdersVo>();
		for (Orders o : orderList) {
			ordersVos.add(Po2Vo.ordersPo2Vo(o));
		}
		return ordersVos;
	}
	@Transactional
	@Override
	public List<String> findWaitingForPickOrdersOidsByCid(String cid) {
		List<Orders> orders=ordersDao.findWaitingForPickOrdersVoByCid(cid);
		List<String> strings=new ArrayList<String>();
		for (Orders orders2 : orders) {
			strings.add(orders2.getoId());
		}
		return strings;
	}
	@Transactional
	@Override
	public OrdersVo findOrderVo(String oid) {
		Orders orders=ordersDao.findOrder(oid);
		return Po2Vo.ordersPo2Vo(orders);
	}

	@Override
	public Merchant calculateNewMerchantGrade(Orders orders) {
		double preMGrade = orders.getMerchant().getGrade();
		Map<Date, Integer> ordersGrades = findOrdersTimeGrade(orders.getMerchant().getmId());
		double mgrade = MerchantGrade.clculateMerchantGrade(preMGrade, ordersGrades, orders.getOgrade(), orders.getDt());
		
		Merchant merchant = orders.getMerchant();
		merchant.setGrade(mgrade);
		
		if (mgrade < 2.5) {
			String message;
			try {
				message = JmsProtocol.serializeMerchantGrade(orders.getMerchant().getmId());
				System.out.println(message);
				queueSender.send(queueMerchantGrade, message);
			} catch (JsonProcessingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return merchant;
	}

}
